package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.file.service.impl.FileStorageServiceImpl;
import com.zhentao.mapper.ApRealnameMapper;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.common.vo.FeedbackVo;
import com.zhentao.model.realname.dto.RealnameDto;
import com.zhentao.model.realname.pojo.ApRealname;
import com.zhentao.model.user.pojo.ApUserInfo;
import com.zhentao.model.user.vo.UserInfoVo;
import com.zhentao.service.ApUserInfoService;
import com.zhentao.mapper.ApUserInfoMapper;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import io.minio.errors.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ap_user_info】的数据库操作Service实现
* @createDate 2024-07-02 11:05:48
*/
@Service
public class ApUserInfoServiceImpl extends ServiceImpl<ApUserInfoMapper, ApUserInfo>
    implements ApUserInfoService{
    @Resource
    ApUserInfoMapper apUserInfoMapper;
    @Resource
    private FileStorageServiceImpl fileStorageService;
    @Resource
    private Configuration configuration;
    @Resource
    ApRealnameMapper apRealnameMapper;

    /**
     *
     * 查询所有用户详情信息--分页--模糊-审核情况条件
     * @param dto
     * @return
     */

    @Override
    public ResponseResult infoList(RealnameDto dto) {
        System.out.println(dto.getPageNum()+"-------------------"+dto.getPageSize()+"");
        Page<ApUserInfo> page=new Page(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ApUserInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(dto.getRealnameName()!=null,ApUserInfo::getRealnameName,dto.getRealnameName());
        wrapper.lambda().eq(dto.getAuditStatus()!=null,ApUserInfo::getAuditStatus,dto.getAuditStatus());
        page = apUserInfoMapper.selectPage(page,wrapper);
        System.err.println(page.getTotal());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }

    /**
     * 查询所有审核通过的用户详细信息（pc）
     * @return
     */
    @Override
    public ResponseResult infoUpList() {
        QueryWrapper<ApUserInfo> wrapper = new QueryWrapper<>();
        //审核状态
        wrapper.lambda().eq(ApUserInfo::getAuditStatus,1);
        List<ApUserInfo> apUserInfos = apUserInfoMapper.selectList(wrapper);
        return ResponseResult.okResult(apUserInfos);
    }


    /**
     * 将用户详情信息添加到minio中
     * @param id
     * @return
     */
//    public ResponseResult createStaticUrl(Long id) throws IOException, TemplateException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
//        ApUserInfo userInfo = apUserInfoMapper.selectById(id);
//        Map<String,Object> map=new HashMap<>();
//        map.put("userInfo",userInfo);
//        Template template = configuration.getTemplate("userInfo.ftl");
//        String s = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//        ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
//        String s1 = fileStorageService.uploadHtmlFile(userInfo.getId() + ".html", inputStream);
//        userInfo.setTextUrl(s1);
//        apUserInfoMapper.updateById(userInfo);
//        return ResponseResult.okResult(s1);
//    }

    /**
     * 用户自主上传个人信息
     * @param apUserInfo
     * @return
     * @throws ServerException
     * @throws TemplateException
     * @throws InvalidBucketNameException
     * @throws InsufficientDataException
     * @throws ErrorResponseException
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws InvalidResponseException
     * @throws XmlParserException
     * @throws InternalException
     */
//    @Override
//    public ResponseResult addUserInfo(ApUserInfo apUserInfo) throws ServerException, TemplateException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
//        QueryWrapper<ApRealname> wrapper=new QueryWrapper<>();
//        wrapper.lambda().eq(ApRealname::getUserId,apUserInfo.getUserId());
//        ApRealname apRealname = apRealnameMapper.selectOne(wrapper);
//        if (apRealname==null){
//            return  ResponseResult.errorResult(301,"还未进行实名，请先实名认证");
//        }
//        //审核状态
//        apUserInfo.setAuditStatus(0);
//        apUserInfo.setCreatedTime(new Date());
//        apUserInfoMapper.insert(apUserInfo);
//        createStaticUrl(Long.valueOf(apUserInfo.getId()));
//        return ResponseResult.okResult("上传成功待审核");
//    }

    /**
     *
     * @param apUserInfo
     * @return
     */
    @Override
    public ResponseResult updateUserInfo(ApUserInfo apUserInfo) throws ServerException, TemplateException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        QueryWrapper<ApRealname> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(ApRealname::getUserId,apUserInfo.getUserId());
        ApRealname apRealname = apRealnameMapper.selectOne(wrapper);
        if (apRealname==null){
            return  ResponseResult.errorResult(301,"还未进行实名，请先实名认证");
        }
        QueryWrapper<ApUserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUserInfo::getUserId,apUserInfo.getUserId());
        apUserInfo.setAuditStatus(0);
        apUserInfo.setUpdatedTime(new Date());
        apUserInfoMapper.update(apUserInfo,queryWrapper);
//        createStaticUrl(Long.valueOf(apUserInfo.getId()));
        return ResponseResult.okResult("更新成功");
    }

    @Override
    public ResponseResult checkPass(Integer id) {
        ApUserInfo apUserInfo = getById(id);
        if (apUserInfo==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户信息不存在");
        }
        apUserInfo.setAuditStatus(1);
        apUserInfoMapper.updateById(apUserInfo);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    public ApUserInfo getById(Integer id){
        ApUserInfo apUserInfo = apUserInfoMapper.selectById(id);
        return apUserInfo;
    }

    @Override
    public ResponseResult checkNotPass(RealnameDto dto) {
        if (dto==null || dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserInfo apUserInfo = getById(dto.getId());
        if (apUserInfo==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户资料不存在");
        }
        apUserInfo.setAuditStatus(2);
        apUserInfoMapper.updateById(apUserInfo);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult infoNotUpList() {
        QueryWrapper<ApUserInfo> wrapper = new QueryWrapper<>();
        //审核状态
        wrapper.lambda().eq(ApUserInfo::getAuditStatus,2);
        List<ApUserInfo> apUserInfos = apUserInfoMapper.selectList(wrapper);
        return ResponseResult.okResult(apUserInfos);
    }

    @Override
    public ResponseResult infoNoPassList() {
        QueryWrapper<ApUserInfo> wrapper = new QueryWrapper<>();
        //审核状态
        wrapper.lambda().eq(ApUserInfo::getAuditStatus,0);
        List<ApUserInfo> apUserInfos = apUserInfoMapper.selectList(wrapper);
        return ResponseResult.okResult(apUserInfos);
    }

    @Override
    public ResponseResult getByUserId(String userId) {
        QueryWrapper<ApUserInfo> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(ApUserInfo::getUserId,userId);
        ApUserInfo apUserInfo = apUserInfoMapper.selectOne(wrapper);
        if (apUserInfo==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        return ResponseResult.okResult(apUserInfo);
    }

    @Override
    public ResponseResult appInfoUpList(UserInfoVo userInfo) {
        QueryWrapper<ApUserInfo> wrapper = new QueryWrapper<>();
        //审核状态
        wrapper.lambda().eq(ApUserInfo::getAuditStatus,1);
        if (userInfo==null){
            List<ApUserInfo> apUserInfos = apUserInfoMapper.selectList(wrapper);
            return ResponseResult.okResult(apUserInfos);
        }
        //姓名模糊
        wrapper.lambda().like(userInfo.getRealnameName()!=null,ApUserInfo::getRealnameName,userInfo.getRealnameName());
        //户籍地模糊
        wrapper.lambda().like(userInfo.getNativePlace()!=null,ApUserInfo::getNativePlace,userInfo.getNativePlace());
        //现居地模糊
        wrapper.lambda().like(userInfo.getPresentAddress()!=null,ApUserInfo::getPresentAddress,userInfo.getPresentAddress());
        //性别
        wrapper.lambda().eq(userInfo.getGender()!=null ,ApUserInfo::getGender,userInfo.getGender());
        //星座
        wrapper.lambda().eq(userInfo.getConstellation()!=null ,ApUserInfo::getConstellation,userInfo.getConstellation());
        //婚姻状况
//        wrapper.lambda().eq(userInfo.getMaritalStatus()!=null ,ApUserInfo::getMaritalStatus,userInfo.getMaritalStatus());
        //学历
        wrapper.lambda().eq(userInfo.getEducation()!=null ,ApUserInfo::getEducation,userInfo.getEducation());
        //房产
        wrapper.lambda().eq(userInfo.getIsHouse()!=null ,ApUserInfo::getIsHouse,userInfo.getIsHouse());
        //车产
        wrapper.lambda().eq(userInfo.getIsVehicle()!=null ,ApUserInfo::getIsVehicle,userInfo.getIsVehicle());
        //民族
        wrapper.lambda().eq(userInfo.getNation()!=null ,ApUserInfo::getNation,userInfo.getNation());
        //年龄区间
        wrapper.lambda().ge(userInfo.getBeginAge()!=null,ApUserInfo::getAge,userInfo.getBeginAge());
        wrapper.lambda().le(userInfo.getEndAge()!=null,ApUserInfo::getAge,userInfo.getEndAge());
        //身高区间
        wrapper.lambda().ge(userInfo.getBeginHeight()!=null,ApUserInfo::getHeight,userInfo.getBeginHeight());
        wrapper.lambda().le(userInfo.getEndHeight()!=null,ApUserInfo::getHeight,userInfo.getEndHeight());
        //体重区间
        wrapper.lambda().ge(userInfo.getBeginWeight()!=null,ApUserInfo::getWeight,userInfo.getBeginWeight());
        wrapper.lambda().le(userInfo.getEndWeight()!=null,ApUserInfo::getWeight,userInfo.getEndWeight());
        //生日区间
        wrapper.lambda().ge(userInfo.getBeginBirth()!=null,ApUserInfo::getBirthdate,userInfo.getBeginBirth());
        wrapper.lambda().le(userInfo.getEndBirth()!=null,ApUserInfo::getBirthdate,userInfo.getEndBirth());
        //工资区间
        wrapper.lambda().ge(userInfo.getBeginSal()!=null,ApUserInfo::getSalary,userInfo.getBeginSal());
        wrapper.lambda().le(userInfo.getEndSal()!=null,ApUserInfo::getSalary,userInfo.getEndSal());

        List<ApUserInfo> apUserInfo = apUserInfoMapper.selectList(wrapper);

        return ResponseResult.okResult(apUserInfo);

    }

    @Override
    public ResponseResult getFeedback(String userId) {
       List<FeedbackVo> feedback= apUserInfoMapper.getFeedback(userId);
       return ResponseResult.okResult(feedback);
    }
}




